import MTObj from './MTObj.js'
import utils from '@/utils/index.js'

// Autotile的默认图集动画数量
const defaultFrameNum = 4

class Autotile extends MTObj {
  constructor (options, rule) {
    super(options)
    this.initAutotile(options, rule)
  }
  
  initAutotile (options, rule) {
    this.type = 'Autotile'
    this.autotile = options.autotile
    this.smallSrc = options.smallSrc
    
    // 判断是否创建动画组
    const isFrames = this.isFrames()
    const standardSrc = utils.str.getParantPath(this.smallSrc) + this.autotile + '_' + rule.name
    const tail = utils.str.getFileTail(this.smallSrc)
    if (!isFrames) {
      // 静态图块
      this.src = standardSrc + '.' + tail
    }
    else {
      // 动态图块,还要创建动画
      this.src = standardSrc + '_0.' + tail
      let srcList = []
      for (let i = 0; i < defaultFrameNum; i++) {
        const src = standardSrc + '_' + i + '.' + tail
        srcList.push(src)
      }
      // 创建默认动画
      this.createAnimation('default', srcList)
    }
  }
  
  // Autotile判断,是否为多帧Autotile
  isFrames () {
    let noTailSrc = utils.str.cutFileTail(this.smallSrc)
    let srcArr = noTailSrc.split('_')
    const tail = srcArr[srcArr.length - 1]
    if (utils.str.isNumberString(tail)) {
      // 判断下划线后的尾缀是否为纯数字,如果是纯数字,则为多帧
      return true
    }
    return false
  }
}

export default Autotile